package org.pushforward.iwillsurvive;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.pushforward.iwillsurvive.data.DatabaseManager;

/**
 * @author Sabre Runner
 *
 */
public class ContextListener implements ServletContextListener
{
	public static DatabaseManager db = DatabaseManager.getInstance();

	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextDestroyed(ServletContextEvent sce)
	{
		DatabaseManager.connectionPool.close();
	}

	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce)
	{
		String dbName = sce.getServletContext().getInitParameter("dbname");
		String dbUsername = sce.getServletContext().getInitParameter("dbusername");
		String dbPassword = sce.getServletContext().getInitParameter("dbpassword");
		db.SetDBName(dbName);
		db.SetUsername(dbUsername);
		db.SetPassword(dbPassword);
		DatabaseManager.appbase = sce.getServletContext().getInitParameter("appbase");
		System.out.println("AppBase: " + DatabaseManager.appbase);
		
		createUsersTable();
		
		createRolesTable();
		
		CreatePostsTable();
		
		CreateGeodesicsTable();
	}		

	/**
	 * Checks for the existence of the users table and if necessary, creates it,
	 * and adds default users
	 * @throws  if some statement threw and SQLException during execution
	 */
	private void createUsersTable()
	{
		String[] usersTable = {"select * from users;",
							   "create table users (email varchar(100) not null, " + 
							   "password varchar(50) not null, " +
							   "name varchar(100), "	+
							   "photosrc varchar(100), "	+
							   "constraint userspk primary key (email));"};
		if (db.ExecuteStatements(usersTable, db.GetEmptyParameters(2)))
		{
			System.out.println("Users table created");
		}
		String[] defaultUsers = {"select * from users where email='srunner';",
							  	 "insert into users values ('srunner','srunner', " +
							  	 "'srunner', '../HAL9000.png');"};
		if (db.ExecuteStatements(defaultUsers, db.GetEmptyParameters(2)))
		{
			System.out.println("Default Users values inserted");
		}
	}

	/**
	 * Checks for the existence of the roles table and if necessary, creates it,
	 * and adds default roles
	 * @throws if some statement threw and SQLException during execution
	 */
	private void createRolesTable()
	{
		String[] rolesTable = {"select * from roles;",
							   "create table roles (email varchar(100) not null, " + 
							   "role varchar(50) not null, " +
							   "primary key (email));"};
		if (db.ExecuteStatements(rolesTable, db.GetEmptyParameters(2)))
		{
			System.out.println("Roles table created.");
		}
		String[] defaultUsers = {"select * from roles where email='srunner';",
							  	 "insert into roles values ('srunner','user');"};
		if (db.ExecuteStatements(defaultUsers, db.GetEmptyParameters(2)))
		{
			System.out.println("Default Roles values inserted.");
		}
	}

	private void CreatePostsTable()
	{
		String[] postsTable = {"select * from posts;",
							   "create table posts (id integer not null, "
							   + "email varchar(100) not null, "
							   + "type boolean not null, "
							   + "geotag varchar(60) not null, "
							   + "time timestamp not null, "
							   + "timelimit timestamp, "
							   + "capacity integer not null, "
							   + "abilities text not null, "
							   + "userslimit integer, "
							   + "users text, "
							   + "content text, "
							   + "primary key (id));"};
	
		if (db.ExecuteStatements(postsTable, db.GetEmptyParameters(2)))
		{
			System.out.println("Posts table created.");
		}
		
		db.UpdateNextID();
	}
	
	private void CreateGeodesicsTable()
	{
		String[] geodesicsTable = {"select * from geodesics;", 
								"create table geodesics (taskid integer not null, " +
								"taskpoint varchar(60) not null, " +
								"teamid integer not null, " + 
								"teampoint varchar(60) not null, " +
								"primary key (taskid));"};

		if (db.ExecuteStatements(geodesicsTable, db.GetEmptyParameters(2)))
		{
			System.out.println("Geodesics table created.");
		}

	}
}

